package com.zeng.plugin.intefaceHandle.impl;

import com.zeng.plugin.entity.TableEntity;
import com.zeng.plugin.intefaceHandle.ParseHandle;
import com.zeng.plugin.util.FreeMarkUtil;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ControllerParseHandle implements ParseHandle {
    private Map<String, Object> data;

    public ControllerParseHandle(Map<String, Object> data) {
        this.data = data;
    }

    public ControllerParseHandle() {

    }
    @Override
    public void parse(List<TableEntity> tables, Map<String, String> mapPackage, String javaPath, String templateFile, FreeMarkUtil freeMarkUtil) {
        for (TableEntity table : tables) {
            Map<String, Object> data = new HashMap<>();
            String className = table.getClassName() + "Controller";
            data.put("controllerPackage", mapPackage.get("controllerPackage"));
            data.put("entityName", table.getClassName());
            data.put("voPackage", mapPackage.get("voPackage"));
            data.put("servicePackage", mapPackage.get("servicePackage"));
            data.put("table", table);
            if (this.data != null) {
                data.put("methodExtend", ((Map<String, List<String>>) this.data.get("methodExtend")).get(table.getRealTableName()));
                data.put("packageExtend", ((Map<String, List<String>>) this.data.get("packageExtend")).get(table.getRealTableName()));
                data.put("propertyExtend", ((Map<String, List<String>>) this.data.get("propertyExtend")).get(table.getRealTableName()));
            }
            try {
                File file = new File(javaPath + "\\" + className + ".java");
                if (!file.exists()) {
                    freeMarkUtil.parse(templateFile, file, data);
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());

            }
        }
    }
}
